export default {
  inserted(el, binding) {
    const value = binding.value || {}
    const {
      delay, // 延时时间
      evtName = 'click', // 事件名
      evt // 事件
    } = value
    const debounce = (fn, delay = 500) => {
      let timer = null
      return function () {
        if (timer) {
          clearTimeout(timer)
        }
        timer = setTimeout(() => {
          fn.apply(this, arguments)
          timer = null
        }, delay)
      }
    }
    if (evtName === 'input') {
      let composing = true
      el.addEventListener(
        'compositionstart',
        () => {
          composing = false
        },
        false
      )
      el.addEventListener(
        'compositionend',
        () => {
          composing = true
        },
        false
      )
      el.addEventListener(
        'input',
        debounce(function (e) {
          if (composing) {
            evt && evt(e.target.value)
          }
        }, delay)
      )
    } else {
      el.addEventListener(
        evtName,
        debounce(function (e) {
          evt && evt()
        }, delay)
      )
    }
  }
}
